package com.zxm;

import com.zxm.web.controller.message.netty.NettyServer;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.websocket.server.WsSci;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;

/**
 * 启动程序
 *
 * @author zxm
 */
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class XmApplication {
	public static void main(String[] args) {
		// System.setProperty("spring.devtools.restart.enabled", "false");
		SpringApplication.run(XmApplication.class, args);
		System.out.println("(♥◠‿◠)ﾉﾞ  小明启动成功   ლ(´ڡ`ლ)ﾞ  \n" +
				" .-------.       ____     __        \n" +
				" |  _ _   \\      \\   \\   /  /    \n" +
				" | ( ' )  |       \\  _. /  '       \n" +
				" |(_ o _) /        _( )_ .'         \n" +
				" | (_,_).' __  ___(_ o _)'          \n" +
				" |  |\\ \\  |  ||   |(_,_)'         \n" +
				" |  | \\ `'   /|   `-'  /           \n" +
				" |  |  \\    /  \\      /           \n" +
				" ''-'   `'-'    `-..-'              ");

		//netty
		try {
			new NettyServer(12345).start();
			System.out.println("http://127.0.0.1:6688/netty-websocket/index");
		}catch(Exception e) {
			System.out.println("NettyServerError:"+e.getMessage());
		}
	}

	/*// 如果没有使用默认值80
	@Value("${http.port:18080}")
	Integer httpPort;

	// 正常启用的https端口 如443
	@Value("${server.port}")
	Integer httpsPort;

	// springboot2 写法
	@Bean
	@ConditionalOnProperty(name = "condition.http2https", havingValue = "true", matchIfMissing = false)
	public TomcatServletWebServerFactory servletContainer() {
		TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
			@Override
			protected void postProcessContext(Context context) {
				SecurityConstraint constraint = new SecurityConstraint();
				constraint.setUserConstraint("CONFIDENTIAL");
				SecurityCollection collection = new SecurityCollection();
				collection.addPattern("/*");
				constraint.addCollection(collection);
				context.addConstraint(constraint);
			}
		};
		tomcat.addAdditionalTomcatConnectors(httpConnector());
		return tomcat;
	}

	@Bean
	@ConditionalOnProperty(name = "condition.http2https", havingValue = "true", matchIfMissing = false)
	public Connector httpConnector() {
		System.out.println("启用http转https协议，http端口：" + this.httpPort + "，https端口：" + this.httpsPort);
		Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
		connector.setScheme("http");
		// Connector监听的http的端口号
		connector.setPort(httpPort);
		connector.setSecure(false);
		// 监听到http的端口号后转向到的https的端口号
		connector.setRedirectPort(httpsPort);
		return connector;
	}*/

	//SpringBoot 2.x版本(以及更高版本) 使用下面的代码
	@Bean
	public ServletWebServerFactory servletContainer() {
		TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
		tomcat.addAdditionalTomcatConnectors(createHTTPConnector());
		return tomcat;
	}

	private Connector createHTTPConnector() {
		Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
		connector.setScheme("http");
		connector.setSecure(false);
		connector.setPort(18081);
		return connector;
	}
	/**
	 * 创建wss协议接口
	 *
	 * @return
	 */
	@Bean
	public TomcatContextCustomizer tomcatContextCustomizer() {
		System.out.println("websocket init");
		return new TomcatContextCustomizer() {
			@Override
			public void customize(Context context) {
				System.out.println("init   customize");
				context.addServletContainerInitializer(new WsSci(), null);
			}
		};
	}
}
